package com.example.controller;

import com.example.model.ResponseResult;
import com.example.service.CacheMetricsCollector;
import com.example.service.MemoryMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统监控控制器
 * 提供系统运行状态、缓存统计、内存使用等监控数据接口
 */
@RestController
@RequestMapping("/api/admin/monitor")
public class SystemMonitorController {

    @Autowired
    private CacheMetricsCollector cacheMetricsCollector;
    
    @Autowired
    private MemoryMonitorService memoryMonitorService;
    
    /**
     * 获取缓存使用统计信息
     */
    @GetMapping("/cache/stats")
    public ResponseResult<Map<String, Object>> getCacheStats() {
        try {
            Map<String, Object> stats = cacheMetricsCollector.getCurrentStats();
            return ResponseResult.success(stats);
        } catch (Exception e) {
            return ResponseResult.error("获取缓存统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 重置缓存统计计数器
     */
    @PostMapping("/cache/reset")
    public ResponseResult<Void> resetCacheStats() {
        try {
            cacheMetricsCollector.resetStats();
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.error("重置缓存统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取内存使用情况
     */
    @GetMapping("/memory/current")
    public ResponseResult<MemoryMonitorService.MemorySnapshot> getCurrentMemoryUsage() {
        try {
            MemoryMonitorService.MemorySnapshot memoryUsage = memoryMonitorService.getCurrentMemoryUsage();
            return ResponseResult.success(memoryUsage);
        } catch (Exception e) {
            return ResponseResult.error("获取内存使用情况失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取内存使用历史记录
     */
    @GetMapping("/memory/history")
    public ResponseResult<Map<String, Object>> getMemoryHistory() {
        try {
            Map<String, Object> result = new HashMap<>();
            result.put("history", memoryMonitorService.getMemoryHistory());
            return ResponseResult.success(result);
        } catch (Exception e) {
            return ResponseResult.error("获取内存历史记录失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取系统概况
     */
    @GetMapping("/summary")
    public ResponseResult<Map<String, Object>> getSystemSummary() {
        try {
            Map<String, Object> summary = new HashMap<>();
            
            // 缓存状态摘要
            Map<String, Object> cacheStats = cacheMetricsCollector.getCurrentStats();
            summary.put("cacheHitRate", cacheStats.get("hitRate"));
            summary.put("totalRequests", cacheStats.get("totalRequests"));
            
            // 内存使用摘要
            MemoryMonitorService.MemorySnapshot memoryUsage = memoryMonitorService.getCurrentMemoryUsage();
            // 使用基于已提交内存的百分比作为主要指标
            summary.put("jvmHeapUsage", memoryUsage.getHeapUsagePercent());
            // 添加基于最大内存的百分比作为辅助指标
            summary.put("jvmHeapMaxUsage", memoryUsage.getHeapMaxUsagePercent());
            // 添加具体数值
            summary.put("jvmHeapUsedBytes", memoryUsage.getHeapUsed());
            summary.put("jvmHeapCommittedBytes", memoryUsage.getHeapCommitted());
            summary.put("jvmHeapMaxBytes", memoryUsage.getHeapMax());
            
            summary.put("redisMemoryUsage", memoryUsage.getRedisUsagePercent());
            
            // 系统运行时间
            summary.put("uptimeMs", cacheStats.get("uptimeMs"));
            
            return ResponseResult.success(summary);
        } catch (Exception e) {
            return ResponseResult.error("获取系统概况失败: " + e.getMessage());
        }
    }
} 